package com.situ.company.department.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.situ.company.department.dao.IDepartmentDao;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.JDBCUtil;

public class DepartmentDaoImpl implements IDepartmentDao {
	public Integer insert(DepartmentModel model) {
		String sql="insert into department(code,name,tel)values(?,?,?)";
		return JDBCUtil.update(sql, model.getCode(),model.getName(),model.getTel());
	}
	public Integer deleteByCode(DepartmentModel model) {
		String sql="delete from department where code=?";
		return JDBCUtil.update(sql, model.getCode());
	}
	public Integer delete(DepartmentModel model) {
		StringBuffer sql=new StringBuffer("delete from department ");
		List<Object> vals=appendWhere(sql, model);
		return JDBCUtil.update(sql.toString(), vals);
	}
	public Integer updateAll(DepartmentModel model) {
		String sql="update department set name=?,tel=? where code=?";
		return JDBCUtil.update(sql, model.getName(),model.getTel());
	}
	public Integer updateActive(DepartmentModel model) {
		StringBuffer sql=new StringBuffer("update department set id=id");
		List<Object> vals=appendUpdate(sql, model);
		System.out.println(sql.toString());
		return JDBCUtil.update(sql.toString(), vals);
	}
	public List<DepartmentModel> selectList(DepartmentModel model) {
		StringBuffer sql=new StringBuffer("select code,name,tel,(select count(1) from employee where code_dept=department.code) count from department");
		List<Object> vals=appendWhere(sql, model);
		
//		System.out.println(sql.toString());
		//映射关系加上count
		List<String> props=Arrays.asList("code","name","tel","count");
		return JDBCUtil.queryList(sql.toString(), vals, DepartmentModel.class,props);
	}
	public DepartmentModel selectModel(DepartmentModel model) {
		StringBuffer sql=new StringBuffer("select code,name,tel from department");
		List<Object> vals=appendWhere(sql, model);
		List<String> props=Arrays.asList("code","name","tel");
		return JDBCUtil.queryModel(sql.toString(), vals, DepartmentModel.class,props);
	}
	public List<Object> appendUpdate(StringBuffer sql,DepartmentModel model){
		List<Object> vals=new ArrayList<>();
		String code=model.getCode();
		if(code!=null) {
			sql.append(",code=?");
			vals.add(code);
		}
		String name=model.getName();
		if(name!=null) {
			sql.append(",name=?");
			vals.add(name);
		}
		String tel=model.getTel();
		if(tel!=null) {
			sql.append(",tel=?");
			vals.add(tel);
		}
		sql.append(" where code=? ");
		vals.add(code);
		return vals;
	}
	
	private List<Object> appendWhere(StringBuffer sql,DepartmentModel model){
		List<Object> vals=new ArrayList<Object>();
		sql.append(" where 1=1 ");
		String code =model.getCode();
		if (!FmtEmpty.isEmpty(code)) {
			sql.append(" and code like ? ");
			vals.add(code);
		}
		String name=model.getName();
		if (!FmtEmpty.isEmpty(name)) {
			sql.append(" and name like ? ");
			vals.add(name);
		}
		String tel=model.getTel();
		if (!FmtEmpty.isEmpty(tel)) {
			sql.append(" and tel like ? ");
			vals.add(tel);
		}
		//添加分页条件
		if(model.isPageOn()) {
			//1-0,10
			//2-10,10
			//3-20,10
			sql.append(" limit ?,?");
			vals.add(model.getRowStart());
			vals.add(model.getPageLimit());
		}
		return vals;
	}
	
	public Integer selectCount(DepartmentModel m0) {
		StringBuffer sql=new StringBuffer("select count(1) from department ");
		List<Object>  vals=appendWhere(sql, m0);
		return JDBCUtil.queryInt(sql.toString(), vals);
	}
}
